<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Stock extends Backend_Controller {
	
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct ();
		$this->isValidRequest ();
		// $this->load->model ( 'backend/Stock_model', 'Stock' );
		$this->load->model ( 'backend/StockOrder_model', 'StockOrder' );
		// 加载验证类
		$this->load->library ( 'form_validation' );
	}
	
	/**
	 * 显示供货商库存
	 */
	public function index() {
		$result = $this->Stock->getStockAll ( $this->page, $this->input->post () );
		$this->load->view ( 'backend/stock/index', $result );
	}
	
	/**
	 * 补仓首页
	 */
	public function fillIndex() {
		$result = $this->StockOrder->getFillStock ( $this->page, $this->input->post (), '0' );
		$this->load->model('backend/Category_model', 'Category');
		
		$this->load->model('backend/Areas_model', 'Areas');
		$result['address'] = $this->Areas->getAreasIN();

		$this->load->view ( 'backend/stockOrder/index', $result );
	}
	
	/**
	 * 补仓审核
	 */
	public function edit() {
		$this->isValidRequest ();
		
		if ($this->isPost ()) {
			$this->form_validation->validateData ( array (
					'id' 
			), array (
					array (
							'id',
							'required|integer' 
					) 
			) );
			
			if ($this->form_validation->hasErrors ()) {
				$this->renderJson ( 300, $this->form_validation->error );
			}
			$stock = $this->post ( 'stock' );
			$data = array ();
			foreach ( $stock as $v ) {
				if (intval ( $v ['number'] ) < 1) {
					$this->renderJson ( 10050 );
				}
				$data [] = array (
						'id' => intval ( $v ['id'] ),
						'actual_number' => intval ( $v ['number'] ) 
				);
			}
			$update = $this->StockOrder->upFillOrder ( $data, $this->form_validation->id, '3' );
			
			if ($update) {
				$this->renderJson ( 200, '', array (
						'navTabId' => 'backendstockfillIndex',
						'callbackType' => 'closeCurrent' 
				) );
			}
		}
		
		$id = intval ( $this->get ( 'id' ) );
		$result ['dataOne'] = $this->StockOrder->findStockOrder ( $id );
		$result ['data'] = $this->StockOrder->findOrderProduct ( $id );
		$this->load->view ( 'backend/stockOrder/edit', $result );
	}
	
	/**
	 * 退仓审核
	 */
	public function backEdit() {
		$this->isValidRequest ();
		
		if ($this->isPost ()) {
			$this->form_validation->validateData ( array (
					'id' 
			), array (
					array (
							'id',
							'required|integer' 
					) 
			) );
			
			if ($this->form_validation->hasErrors ()) {
				$this->renderJson ( 300, $this->form_validation->error );
			}
			$stock = $this->post ( 'stock' );
			$data = array ();
			foreach ( $stock as $v ) {
				if (intval ( $v ['number'] ) < 1) {
					$this->renderJson ( 10050 );
				}
				$data [] = array (
						'id' => intval ( $v ['id'] ),
						'actual_number' => intval ( $v ['number'] ),
						'product_id' => intval ( $v ['product_id'] ) 
				);
			}
			$update = $this->StockOrder->upBackOrder ( $data, $this->form_validation->id, '10' );
			
			if ($update) {
				$this->renderJson ( 200, '', array (
						'navTabId' => 'backendstockbackIndex',
						'callbackType' => 'closeCurrent' 
				) );
			}
		}
		
		$id = intval ( $this->get ( 'id' ) );
		$result ['dataOne'] = $this->StockOrder->findStockOrder ( $id );
		$result ['data'] = $this->StockOrder->findOrderProduct ( $id );
		$this->load->view ( 'backend/stockOrder/backEdit', $result );
	}
	
	/**
	 * 退仓首页
	 */
	public function backIndex() {
		$result = $this->StockOrder->getFillStock ( $this->page, $this->input->post (), '1' );
		
		$this->load->model('backend/Areas_model', 'Areas');
		$result['address'] = $this->Areas->getAreasIN();
		$this->load->view ( 'backend/stockOrder/backIndex', $result );
	}
	
	/**
	 * 总仓确认补仓订单
	 */
	public function backEditok() {
		$this->isValidRequest ();
		$id = intval ( $this->get ( 'id' ) );
		$data = array (
				'status' => '3' 
		);
		
		$update = $this->StockOrder->upBackOrderStatus ( $data, $id );
		if ($update) {
			$result = $this->StockOrder->getFillStock ( $this->page, $this->input->post (), '1' );
			$this->load->view ( 'backend/stockOrder/backIndex', $result );
		}
	}
}
